package com.tanjie.blackbean.web.support;

import com.tanjie.blackbean.pojo.auth.AuthConst;
import com.tanjie.blackbean.web.annotation.BlackBeanController;
import com.tanjie.blackbean.web.annotation.SkipAuth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.method.HandlerMethod;

import java.util.List;

import static com.tanjie.blackbean.core.util.UrlPathUtil.skip;

/**
 * @Author tanjie
 * @Date 2022/6/22 下午3:55 周三
 **/
@Slf4j
public abstract class UrlSkipUtil {

    private UrlSkipUtil() {

    }

    public static boolean annotationSkipCheck(String uri, Object handler) {
        if (handler instanceof HandlerMethod) {
            SkipAuth skipAuth = AnnotationExtractor.get((HandlerMethod) handler, SkipAuth.class);
            if (skipAuth != null) {
                log.debug("annotationSkipCheck: uri={} @SkipAuth跳过校验", uri);
                return true;
            }
            BlackBeanController blackBeanController = AnnotationExtractor.get((HandlerMethod) handler, BlackBeanController.class);
            if (blackBeanController != null) {
                if (blackBeanController.skipAuth()) {
                    log.debug("annotationSkipCheck: uri={} @BlackBeanController注解配置了跳过校验", uri);
                    return true;
                }
                if (blackBeanController.isFeign()) {
                    log.debug("annotationSkipCheck: uri={} feign调用，跳过校验", uri);
                    return true;
                }
            }
        }
        log.debug("annotationSkipCheck: uri={} {}", uri, ">>>> no skip match <<<<");
        return false;
    }

    public static boolean skipUrlCheck(String uri, List<String> skipUrlList, String method) {
        if (skip(uri, skipUrlList) || skip(uri, AuthConst.DEFAULT_SKIP_URL)) {
            log.debug("skipUrlCheck: uri={} method={} {}", uri, method, "skip match");
            return true;
        }
        log.debug("skipUrlCheck: uri={} method={} {}", uri, method, ">>>> no skip match <<<<");
        return false;
    }


}
